package cn.edu.gzccc.movie.Controller;

import cn.edu.gzccc.movie.domain.Actor;
import cn.edu.gzccc.movie.service.ActorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/actor")
public class ActorController {

    @Autowired
    private ActorService actorService;

    /**
     * 演员列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/index/{page}",method = RequestMethod.GET)
    public String index(Model model,@PathVariable Integer page) {
        Page<Actor> list = actorService.findActorNoCriteria(page,10);
        model.addAttribute("actors", list);
        return "actor/index";
    }

    /**
     * 跳转到新增演员页面
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toAdd() {
        return "actor/add";
    }

    /**
     * 保存新增演员到数据库，重定向到演员列表页面
     *
     * @param actor
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String toAdd(Actor actor,BindingResult bindingResult) {
        actorService.add(actor);
        return "redirect:/actor/index/0";
    }

    /**
     * '
     * 跳转到演员编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id) {
        Actor actor = actorService.findOne(id);
        model.addAttribute("info", actor);
        return "actor/edit";
    }

    /**
     * 保存编辑页面修改的信息，重定向到演员列表页面
     *
     * @param actor
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(@RequestParam Actor actor) {
        actorService.update(actor);
        return "redirect:/actor/index";
    }

    /**
     * 删除演员
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delect/{id}", method = RequestMethod.GET)
    public String delect(@PathVariable Long id) {
        actorService.delete(id);
        return "redirect:/actor/index/0";
    }
}